package com.zwr.sheller.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwr.sheller.core.common.JsonResult;
import com.zwr.sheller.core.common.LJCBusinessException;
import com.zwr.sheller.core.common.LJCExceptionCode;
import com.zwr.sheller.core.pojo.*;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.core.util.OSSUtil;
import com.zwr.sheller.core.util.SnowflakeUtil;
import com.zwr.sheller.core.util.ThreadLocalUtil;
import com.zwr.sheller.shop.controller.OSSController;
import com.zwr.sheller.shop.mapper.*;
import com.zwr.sheller.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ljc
 * @date 2022/1/7
 **/
@Service("goodsService")
@Transactional(rollbackFor = Throwable.class)
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired(required = false)
    GoodsMapper goodsMapper;

    @Autowired(required = false)
    FeatureMapper featureMapper;

    @Autowired(required = false)
    StartPriceMapper startPriceMapper;

    @Autowired(required = false)
    LabelMapper labelMapper;

    @Autowired(required = false)
    LabelGroupMapper labelGroupMapper;

    @Autowired(required = false)
    SkuMapper skuMapper;

    @Autowired(required = false)
    GoodsParamMapper goodsParamMapper;

    @Autowired(required = false)
    GoodsDetailMapper goodsDetailMapper;

    @Autowired(required = false)
    MarketingMapper marketingMapper;

    @Autowired(required = false)
    AssociateGoodsMapper associateGoodsMapper;

    @Autowired(required = false)
    ItemMapper itemMapper;

    @Autowired(required = false)
    GoodsGroupMapper goodsGroupMapper;
            
    //获取店铺编号
    Store store = (Store) ThreadLocalUtil.getObject();
    //Long storeId = store.getStoreId();
    Long storeId = 1L;

    public static final String GOODSDETAILINFO = "goodsDetailInfo:";

    @Override
    public Map getGoodsClassification() {
        //分别获取3个分类的名称集合
        List<Map<String, Object>> list1 = goodsMapper.selectClassificationName(storeId, 1);
        List<Map<String, Object>> list2 = goodsMapper.selectClassificationName(storeId, 2);
        List<Map<String, Object>> list3 = goodsMapper.selectClassificationName(storeId, 3);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("list1", list1);
        jsonResult.setField("list2", list2);
        jsonResult.setField("list3", list3);
        return jsonResult.getMap();
    }

    @Override
    public Map getGoodsAllClassification() {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andEqualTo("itemDisplay", true);
        List<Item> items = itemMapper.selectByExample(example);
        for (Item item : items) {
            HashMap<String, Object> map = new HashMap<>(10);
            //获取item分类名
            String itemName = item.getItemName();
            //判断item是否有父级分类
            if (item.getItemParentId() != null) {
                //如果item有父级分类，获得其父级分类对象
                Item fatherItem = itemMapper.selectByPrimaryKey(item.getItemParentId());
                //判断父级分类是否还有父级分类
                if (fatherItem.getItemParentId() != null) {
                    //如果有说明item是3级分类，fatherItem是2级分类
                    //获取1级分类对象
                    Item firstItem = itemMapper.selectByPrimaryKey(fatherItem.getItemParentId());
                    //获取1级分类名
                    String firstItemName = firstItem.getItemName();
                    //获取2级分类名
                    String secondItemName = fatherItem.getItemName();
                    map.put("itemId", item.getItemId());
                    map.put("item", firstItemName + "-" + secondItemName + "-" + itemName);
                    map.put("rank", item.getItemRank());
                    list.add(map);
                } else {
                    //如果没有说明item是2级分类，fatherItem是1级分类
                    //获取1级分类名
                    String firstItemName = fatherItem.getItemName();
                    map.put("itemId", item.getItemId());
                    map.put("item", firstItemName + "-" + itemName);
                    map.put("rank", item.getItemRank());
                    list.add(map);
                }
            } else {
                //说明item是1级分类
                map.put("itemId", item.getItemId());
                map.put("item", itemName);
                map.put("rank", item.getItemRank());
                list.add(map);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", list);
        return jsonResult.getMap();
    }

    @Override
    public Map addGoodsInfo(Map<String, Object> map) throws LJCBusinessException {
        //获取goods对象
        String goodsString = JsonUtil.objectToString(map.get("goods"));
        Goods goods = JsonUtil.stringToObject(goodsString, Goods.class);
        //调用上传图片的工具方法得到图片真实路径
        String realImgPath = uploadImg(goods.getGoodsMainImg());
        goods.setGoodsMainImg(realImgPath);
        //雪花算法生成商品编号
        Long goodsId = SnowflakeUtil.nextId();
        goods.setGoodsId(goodsId);
        Integer goodsIsOnShelf = goods.getGoodsIsOnShelf();
        //判断商品是否上架
        if (goodsIsOnShelf == 2) {
            //商品入库
            goods.setGoodsShelfStatus(false);
        } else {
            //直接上架/定时上架
            goods.setGoodsShelfStatus(true);
            goods.setGoodsShelfTime(new Timestamp(System.currentTimeMillis()));
        }
        goods.setGoodsIsDelete(false);
        goods.setStoreId(storeId);
        //插入商品表
        goodsMapper.insertSelective(goods);
        //获取startPrice对象
        Map<String, Object> startPriceMap = (Map<String, Object>) map.get("startPrice");
        //获取起批价起始个数集合
        List<String> numList = (List<String>) startPriceMap.get("num");
        //获取起批价单价集合
        List<String> onePriceList = (List<String>) startPriceMap.get("onePrice");
        //获取起批价原价集合
        List<String> originalList = (List<String>) startPriceMap.get("originalPrice");
        //遍历记录数
        for (int i = 0; i < numList.size(); i++) {
            StartPrice startPrice = new StartPrice();
            startPrice.setGoodsId(goodsId);
            startPrice.setStartPriceStartNum(Long.parseLong(numList.get(i)));
            startPrice.setStartPriceOnePrice(Double.parseDouble(onePriceList.get(i)));
            startPrice.setStartPriceOriginalPrice(Double.parseDouble(originalList.get(i)));
            //插入起批价表
            startPriceMapper.insertSelective(startPrice);
        }
        //获取featureImg对象
        List<String> featureImgList = (List<String>) map.get("featureImg");
        for (String s : featureImgList) {
            Feature feature = new Feature();
            feature.setGoodsId(goodsId);
            //获取真实图片地址
            String s1 = uploadImg(s);
            feature.setFeatureImg(s1);
            //插入特色图表
            featureMapper.insertSelective(feature);
        }
        //获取sku对象
        Map<String, Object> skuMap = (Map<String, Object>) map.get("sku");
        //获取规格名称
        String specificationName = skuMap.get("specificationName").toString();
        //获取sku名称集合
        List<String> skuNameList = (List<String>) skuMap.get("skuName");
        //获取sku库存集合
        List<String> skuStockList = (List<String>) skuMap.get("skuStock");
        //获取sku单价集合
        List<String> skuPriceList = (List<String>) skuMap.get("skuPrice");
        //获取sku单位集合
        List<String> skuUnitList = (List<String>) skuMap.get("skuUnit");
        //获取sku数量集合
        List<String> skuNumList = (List<String>) skuMap.get("skuNum");
        //获取sku重量集合
        List<String> skuWeightList = (List<String>) skuMap.get("skuWeight");
        //遍历sku属性进行插入表
        for (int i = 0; i < skuNameList.size(); i++) {
            Sku sku = new Sku();
            sku.setGoodsId(goodsId);
            sku.setSkuSpecificationName(specificationName);
            sku.setSkuName(skuNameList.get(i));
            sku.setSkuStock(Long.parseLong(skuStockList.get(i)));
            sku.setSkuPrice(Double.parseDouble(skuPriceList.get(i)));
            sku.setSkuUnit(skuUnitList.get(i));
            sku.setSkuNum(Long.parseLong(skuNumList.get(i)));
            sku.setSkuWeight(Double.parseDouble(skuWeightList.get(i)));
            //插入sku表
            skuMapper.insertSelective(sku);
        }
        //获取param对象
        Map<String, Object> paramMap = (Map<String, Object>) map.get("param");
        //获取paramName集合
        List<String> paramNameList = (List<String>) paramMap.get("paramName");
        //获取value集合
        List<String> valueList = (List<String>) paramMap.get("value");
        //遍历记录数
        for (int i = 0; i < paramNameList.size(); i++) {
            GoodsParam goodsParam = new GoodsParam();
            goodsParam.setGoodsId(goodsId);
            goodsParam.setGoodsParamName(paramNameList.get(i));
            goodsParam.setGoodsParamValue(valueList.get(i));
            //插入goods_Param表
            goodsParamMapper.insertSelective(goodsParam);
        }
        //获取goodsDetailImg对象
        List<String> goodsDetailImgList = (List<String>) map.get("goodsDetailImg");
        for (String s : goodsDetailImgList) {
            GoodsDetail goodsDetail = new GoodsDetail();
            String s1 = uploadImg(s);
            goodsDetail.setGoodsId(goodsId);
            goodsDetail.setGoodsDetailImg(s1);
            //插入goods_detail表
            goodsDetailMapper.insertSelective(goodsDetail);
        }
        //获取marketing对象
        String marketingString = JsonUtil.objectToString(map.get("marketing"));
        Marketing marketing = JsonUtil.stringToObject(marketingString, Marketing.class);
        marketing.setGoodsId(goodsId);
        //插入marketing表
        marketingMapper.insertSelective(marketing);
        //获取associateGoods对象
        List<String> associateGoodsList = (List<String>) map.get("associateGoods");
        //遍历关联商品记录数
        for (String s : associateGoodsList) {
            AssociateGoods associateGoods = new AssociateGoods();
            associateGoods.setIsAssociatedGoods(goodsId);
            associateGoods.setAssociatedGoods(Long.parseLong(s));
            //拆入associateGoods表
            associateGoodsMapper.insertSelective(associateGoods);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map selectGoodsWithPage(Long itemId, String goodsNameOrKeyWord, Integer rank, String goodsLabel, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Map<String, Object>> list = selectGoodsNoPage(itemId, goodsNameOrKeyWord, rank, goodsLabel);
        //遍历整个list
        for (Map<String, Object> map : list) {
            //将商品编号类型改为String
            map.replace("goodsId", map.get("goodsId").toString());
            if ("false".equals(map.get("shelfStatus").toString()) && map.get("shelfTime") == null) {
                //如果为下架状态且上架时间为null，修改商品状态为待审核
                map.replace("shelfStatus", "待审核");
            } else if ("false".equals(map.get("shelfStatus").toString())) {
                //如果商品状态为false，修改商品状态为下架
                map.replace("shelfStatus", "下架");
            } else if (Long.parseLong(map.get("stock").toString()) == 0) {
                //如果现有库存为0，修改商品状态为已售罄
                map.replace("shelfStatus", "已售罄");
            } else if (Long.parseLong(map.get("warnStock").toString()) >= Long.parseLong(map.get("stock").toString())) {
                //如果现有库存小于预警库存，修改商品状态为库存预警
                map.replace("shelfStatus", "库存预警");
            } else if ("true".equals(map.get("shelfStatus").toString())) {
                //如果商品状态为true，修改商品状态为上架
                map.replace("shelfStatus", "上架");
            }
            map.remove("shelfTime");
        }
        PageInfo of = PageInfo.of(list);
        //获取真正的list
        List realList = of.getList();
        //获取总记录数
        long total = of.getTotal();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", realList);
        jsonResult.setField("total", total);
        return jsonResult.getMap();
    }

    @Override
    public List<Map<String, Object>> selectGoodsNoPage(Long itemId, String goodsNameOrKeyWord, Integer rank, String goodsLabel) {
        //查询商品基本信息
        List<Map<String, Object>> list1 = goodsMapper.selectGoodsNoPage1(itemId, goodsNameOrKeyWord, rank, goodsLabel, storeId);
        //查询商品sku属性
        List<Map<String, Object>> list2 = goodsMapper.selectGoodsNoPage2(itemId, goodsNameOrKeyWord, rank, goodsLabel, storeId);
        //将list2中符合条件的数据插入到list1
        for (Map<String, Object> map1 : list1) {
            Object goodsId1 = map1.get("goodsId");
            for (Map<String, Object> map2 : list2) {
                Object goodsId2 = map2.get("goodsId");
                if (goodsId1.equals(goodsId2)) {
                    map1.putIfAbsent("unit", map2.get("unit"));
                    map1.putIfAbsent("price", map2.get("price"));
                    map1.putIfAbsent("num", map2.get("num"));
                }
            }
        }
        return list1;
    }

    @Override
    public Map getParentClassification() {
        //获取所有的1、2级分类名称
        List<String> list = goodsMapper.getParentClassification(storeId);
        list.add("顶级分类");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", list);
        return jsonResult.getMap();
    }

    @Override
    public Map addClassification(Long sort, String parentClassification, String name, String keyWord, String imgPath, Boolean isRelevance, Boolean isDisplay, Boolean isRecommend, String desc) throws LJCBusinessException {
        String realImgPath = null;
        Example example = new Example(Item.class);
        //通过前端传入的分类名进行查询数据库看分类名是否已存在
        example.createCriteria().andEqualTo("itemName", name);
        List<Item> items = itemMapper.selectByExample(example);
        if (items.size() > 0){
            //分类名已存在抛出异常
            throw new LJCBusinessException(LJCExceptionCode.ITEM_NAME_EXIST);
        }
        if (!StringUtils.isEmpty(imgPath)) {
            //调用上传图片的工具方法
            realImgPath = uploadImg(imgPath);
        }
        //设置默认添加分类为1级分类
        Long parentId = null;
        Integer rank = 1;
        //判断前端传入的上级分类名称是否为1级分类
        if (!"顶级分类".equals(parentClassification)) {
            //如果不是1级分类则从数据库查询获得其分类编号及分类级别
            Map<String, Object> map = goodsMapper.selectParentIdAndRankByName(parentClassification, storeId);
            parentId = (Long) map.get("item_id");
            Integer parentRank = (Integer) map.get("item_rank");
            rank = parentRank + 1;
        }
        goodsMapper.addClassification(sort, parentId, name, keyWord, realImgPath, isRelevance, isDisplay, isRecommend, desc, storeId, rank);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功!");
        return jsonResult.getMap();
    }

    @Override
    public Map deleteGoodsClassification(Long classificationId) throws LJCBusinessException {
        //判断前端传入的分类编号下是否还有子分类
        List<Map<String, Object>> list = goodsMapper.selectParentIdIsExist(classificationId);
        //判断该分类下是否还有关联的商品
        Example example = new Example(Goods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("goodsFirstItemId", classificationId);
        criteria.orEqualTo("goodsSecondItemId", classificationId);
        criteria.orEqualTo("goodsThirdItemId", classificationId);
        List<Goods> goods = goodsMapper.selectByExample(example);
        if (list.size() > 0 || goods.size() > 0) {
            //如果还有子分类或者该分类还有关联的商品则抛出异常
            throw new LJCBusinessException(LJCExceptionCode.ITEM_HAS_SUB);
        }
        //没有子分类则执行删除操作
        goodsMapper.deleteGoodsClassification(classificationId, storeId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @Override
    public Map addLabelGroup(Map<String, Object> map) throws LJCBusinessException {
        //获取标签组对象
        String jsonString = JsonUtil.objectToString(map.get("labelGroup"));
        LabelGroup labelGroup = JsonUtil.stringToObject(jsonString, LabelGroup.class);
        labelGroup.setStoreId(storeId);
        //获取标签组名
        String labelGroupName = labelGroup.getLabelGroupName();
        //获取插入标签组的编号
        Long groupId = goodsMapper.selectLabelGroupIdByLabelGroupName(labelGroupName);
        //判断新添加的标签组名是否存在
        if (groupId != null) {
            //如果存在则抛出异常
            throw new LJCBusinessException(LJCExceptionCode.LABEL_GROUP_NAME_EXIST);
        }
        //插入表
        labelGroupMapper.insertSelective(labelGroup);
        //获取插入标签组的编号
        Long groupId1 = goodsMapper.selectLabelGroupIdByLabelGroupName(labelGroupName);

        //获取标签名集合
        List<String> labelsList = (List<String>) map.get("labelName");
        //遍历标签名集合进行插入表
        for (String s : labelsList) {
            Label label = new Label();
            label.setLabelsName(s);
            label.setLabelsGroupId(groupId1);
            labelMapper.insertSelective(label);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map findLabelGroup() {
        Example example = new Example(LabelGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("labelGroupStatus", true);
        criteria.andEqualTo("storeId", storeId);
        List<LabelGroup> labelGroups = labelGroupMapper.selectByExample(example);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", labelGroups);
        return jsonResult.getMap();
    }

    @Override
    public Map deleteGoods(Map<String, Object> map) throws LJCBusinessException {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        Integer result = goodsMapper.deleteGoods(goodsIdList);
        if (result < 0) {
            throw new LJCBusinessException(LJCExceptionCode.DELETE_FAILED);
        }
        //遍历商品编号集合，删除商品缓存
        for (String goodsId : goodsIdList) {
            redisTemplate.delete(GOODSDETAILINFO + goodsId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @Override
    public Map downShelfGoods(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        goodsMapper.upOrDownShelfGoods(goodsIdList, "down", null);
        //遍历商品编号集合，删除商品缓存
        for (String goodsId : goodsIdList) {
            redisTemplate.delete(GOODSDETAILINFO + goodsId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("下架成功");
        return jsonResult.getMap();
    }

    @Override
    public Map upShelfGoods(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        ArrayList<String> hasShelfTimeList = new ArrayList<>();
        ArrayList<String> noShelfTimeList = new ArrayList<>();
        for (String goodsId : goodsIdList) {
            //查询数据库获取商品对象
            Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
            if (!StringUtils.isEmpty(goods.getGoodsShelfTime())) {
                //如果上架的商品已经有上架时间则将其放入hasShelfTimeList
                hasShelfTimeList.add(goodsId);
            } else {
                //没有上架时间将其放入noShelfTimeList
                noShelfTimeList.add(goodsId);
            }
        }
        if (hasShelfTimeList.size() > 0) {
            //如果有上架时间的集合有数据则执行
            goodsMapper.upOrDownShelfGoods(hasShelfTimeList, "up", null);
        }
        if (noShelfTimeList.size() > 0) {
            //如果没有上架时间的集合有数据则执行
            goodsMapper.upOrDownShelfGoods(noShelfTimeList, "up", new Timestamp(System.currentTimeMillis()));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("上架成功");
        return jsonResult.getMap();
    }

    @Override
    public Map getThirdClassification() {
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andEqualTo("itemDisplay", true);
        //获取该店铺所有的分类
        List<Item> items = itemMapper.selectByExample(example);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (Item item : items) {
            HashMap<String, Object> map = new HashMap<>(10);
            //判断item是否有父级
            if (!StringUtils.isEmpty(item.getItemParentId())) {
                //如果item有父级,判断其父级是否还有父级
                Item fatherItem = itemMapper.selectByPrimaryKey(item.getItemParentId());
                if (!StringUtils.isEmpty(fatherItem.getItemParentId())) {
                    //如果fatherItem还有父级，说明item是3级分类，fatherItem是2级分类
                    //获取一级分类对象
                    Item firstItem = itemMapper.selectByPrimaryKey(fatherItem.getItemParentId());
                    map.put("firstItemId", firstItem.getItemId());
                    map.put("secondItemId", fatherItem.getItemId());
                    map.put("thirdItemId", item.getItemId());
                    map.put("item", firstItem.getItemName() + "-" + fatherItem.getItemName() + "-" + item.getItemName());
                    list.add(map);
                }
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", list);
        return jsonResult.getMap();
    }

    @Override
    public Map updateGoodsClassification(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        //分别获取商品3个分类编号
        long firstItemId = Long.parseLong(map.get("firstItemId").toString());
        long secondItemId = Long.parseLong(map.get("secondItemId").toString());
        long thirdItemId = Long.parseLong(map.get("thirdItemId").toString());
        goodsMapper.updateGoodsClassification(goodsIdList, firstItemId, secondItemId, thirdItemId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @Override
    public Map updateGoodsLabel(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        //获取3个商品标签
        String label1 = map.get("label1").toString();
        String label2 = map.get("label2").toString();
        String label3 = map.get("label3").toString();
        //获取标签组编号
        if (StringUtils.isEmpty(map.get("labelGroupId"))) {
            goodsMapper.updateGoodsLabel(goodsIdList, label1, label2, label3, null);
        } else {
            long labelGroupId = Long.parseLong(map.get("labelGroupId").toString());
            goodsMapper.updateGoodsLabel(goodsIdList, label1, label2, label3, labelGroupId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @Override
    public Map realDeleteGoods(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        for (String goodsId : goodsIdList) {
            //删除关联商品表数据
            Example example1 = new Example(AssociateGoods.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.orEqualTo("isAssociatedGoods", goodsId);
            criteria1.orEqualTo("associatedGoods", goodsId);
            associateGoodsMapper.deleteByExample(example1);
            //删除特色图表数据
            Example example2 = new Example(Feature.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.orEqualTo("goodsId", goodsId);
            featureMapper.deleteByExample(example2);
            //删除商品详情表数据
            Example example3 = new Example(GoodsDetail.class);
            Example.Criteria criteria3 = example3.createCriteria();
            criteria3.orEqualTo("goodsId", goodsId);
            goodsDetailMapper.deleteByExample(example3);
            //删除商品参数表数据
            Example example4 = new Example(GoodsParam.class);
            Example.Criteria criteria4 = example4.createCriteria();
            criteria4.orEqualTo("goodsId", goodsId);
            goodsParamMapper.deleteByExample(example4);
            //删除营销表数据
            Example example5 = new Example(Marketing.class);
            Example.Criteria criteria5 = example5.createCriteria();
            criteria5.orEqualTo("goodsId", goodsId);
            marketingMapper.deleteByExample(example5);
            //删除sku表数据
            Example example6 = new Example(Sku.class);
            Example.Criteria criteria6 = example6.createCriteria();
            criteria6.orEqualTo("goodsId", goodsId);
            skuMapper.deleteByExample(example6);
            //删除起批价表数据
            Example example7 = new Example(StartPrice.class);
            Example.Criteria criteria7 = example7.createCriteria();
            criteria7.orEqualTo("goodsId", goodsId);
            startPriceMapper.deleteByExample(example7);
            //删除商品表数据
            goodsMapper.deleteByPrimaryKey(goodsId);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @Override
    public Map recoverGoods(Map<String, Object> map) {
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        goodsMapper.recoverGoods(goodsIdList);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("还原成功");
        return jsonResult.getMap();
    }

    @Override
    public Map getGoodsInfo(String goodsId) {
        //获取商品对象
        Goods goods = new Goods();
        goods.setGoodsId(Long.parseLong(goodsId));
        List<Goods> goodsList = goodsMapper.select(goods);
        //获取商品特色图对象
        Feature feature = new Feature();
        feature.setGoodsId(Long.parseLong(goodsId));
        List<Feature> featureList = featureMapper.select(feature);
        //获取商品详情对象
        GoodsDetail goodsDetail = new GoodsDetail();
        goodsDetail.setGoodsId(Long.parseLong(goodsId));
        List<GoodsDetail> goodsDetailList = goodsDetailMapper.select(goodsDetail);
        //获取商品参数对象
        GoodsParam goodsParam = new GoodsParam();
        goodsParam.setGoodsId(Long.parseLong(goodsId));
        List<GoodsParam> goodsParamList = goodsParamMapper.select(goodsParam);
        //获取营销对象
        Marketing marketing = new Marketing();
        marketing.setGoodsId(Long.parseLong(goodsId));
        List<Marketing> marketingList = marketingMapper.select(marketing);
        //获取商品sku对象
        Sku sku = new Sku();
        sku.setGoodsId(Long.parseLong(goodsId));
        List<Sku> skuList = skuMapper.select(sku);
        //获取商品起批价对象
        StartPrice startPrice = new StartPrice();
        startPrice.setGoodsId(Long.parseLong(goodsId));
        List<StartPrice> startPriceList = startPriceMapper.select(startPrice);
        HashMap<String, Object> map = new HashMap<>(10);
        map.put("goods", goodsList);
        map.put("feature", featureList);
        map.put("goodsDetail", goodsDetailList);
        map.put("goodsParam", goodsParamList);
        map.put("marketing", marketingList);
        map.put("sku", skuList);
        map.put("startPrice", startPriceList);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", map);
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsBasicInfo(Map<String, Object> map) throws LJCBusinessException {
        //获取goods对象
        String goodsString = JsonUtil.objectToString(map.get("goods"));
        Goods goods = JsonUtil.stringToObject(goodsString, Goods.class);
        Long goodsId = goods.getGoodsId();
        if (!StringUtils.isEmpty(goods.getGoodsMainImg())) {
            //如果商品主图被修改
            //调用上传图片的工具方法得到图片真实路径
            String realImgPath = uploadImg(goods.getGoodsMainImg());
            goods.setGoodsMainImg(realImgPath);
        }
        //修改goods表
        goodsMapper.updateByPrimaryKeySelective(goods);
        //删除起批价表中商品的数据并进行重新添加
        Example example = new Example(StartPrice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", goodsId);
        startPriceMapper.deleteByExample(example);
        //获取startPrice对象
        Map<String, Object> startPriceMap = (Map<String, Object>) map.get("startPrice");
        //获取起批价起始个数集合
        List<String> numList = (List<String>) startPriceMap.get("num");
        //获取起批价单价集合
        List<String> onePriceList = (List<String>) startPriceMap.get("onePrice");
        //获取起批价原价集合
        List<String> originalList = (List<String>) startPriceMap.get("originalPrice");
        //遍历记录数
        for (int i = 0; i < numList.size(); i++) {
            StartPrice startPrice = new StartPrice();
            startPrice.setGoodsId(goodsId);
            startPrice.setStartPriceStartNum(Long.parseLong(numList.get(i)));
            startPrice.setStartPriceOnePrice(Double.parseDouble(onePriceList.get(i)));
            startPrice.setStartPriceOriginalPrice(Double.parseDouble(originalList.get(i)));
            //插入起批价表
            startPriceMapper.insertSelective(startPrice);
        }
        //获取feature对象
        List<String> featureImgList = (List<String>) map.get("featureImg");
        if (featureImgList.size() > 0) {
            //如果feature有修改则先删除所有此商品的特色图再重新添加
            Example example1 = new Example(Feature.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("goodsId", goodsId);
            featureMapper.deleteByExample(example1);
            for (String s : featureImgList) {
                Feature feature = new Feature();
                feature.setGoodsId(goodsId);
                //获取真实图片地址
                String s1 = uploadImg(s);
                feature.setFeatureImg(s1);
                //插入特色图表
                featureMapper.insertSelective(feature);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsStock(Map<String, Object> map) {
        //获取goods对象
        String goodsString = JsonUtil.objectToString(map.get("goods"));
        Goods goods = JsonUtil.stringToObject(goodsString, Goods.class);
        goodsMapper.updateByPrimaryKeySelective(goods);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsSpecification(Map<String, Object> map) {
        //获取sku对象
        Map<String, Object> skuMap = (Map<String, Object>) map.get("sku");
        //获取商品编号
        long goodsId = Long.parseLong(skuMap.get("goodsId").toString());
        //先删除数据库中sku数据，再重新添加
        Example example = new Example(Sku.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", goodsId);
        skuMapper.deleteByExample(example);
        //获取规格名称
        String specificationName = skuMap.get("specificationName").toString();
        //获取sku名称集合
        List<String> skuNameList = (List<String>) skuMap.get("skuName");
        //获取sku库存集合
        List<String> skuStockList = (List<String>) skuMap.get("skuStock");
        //获取sku单价集合
        List<String> skuPriceList = (List<String>) skuMap.get("skuPrice");
        //获取sku单位集合
        List<String> skuUnitList = (List<String>) skuMap.get("skuUnit");
        //获取sku数量集合
        List<String> skuNumList = (List<String>) skuMap.get("skuNum");
        //获取sku重量集合
        List<String> skuWeightList = (List<String>) skuMap.get("skuWeight");
        //遍历sku属性进行插入表
        for (int i = 0; i < skuNameList.size(); i++) {
            Sku sku = new Sku();
            sku.setGoodsId(goodsId);
            sku.setSkuSpecificationName(specificationName);
            sku.setSkuName(skuNameList.get(i));
            sku.setSkuStock(Long.parseLong(skuStockList.get(i)));
            sku.setSkuPrice(Double.parseDouble(skuPriceList.get(i)));
            sku.setSkuUnit(skuUnitList.get(i));
            sku.setSkuNum(Long.parseLong(skuNumList.get(i)));
            sku.setSkuWeight(Double.parseDouble(skuWeightList.get(i)));
            //插入sku表
            skuMapper.insertSelective(sku);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsParameters(Map<String, Object> map) {
        //获取param对象
        Map<String, Object> paramMap = (Map<String, Object>) map.get("param");
        //获取商品编号
        long goodsId = Long.parseLong(paramMap.get("goodsId").toString());
        //先删除数据库goodsParam表数据再重新添加
        Example example = new Example(GoodsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsId", goodsId);
        goodsParamMapper.deleteByExample(example);
        //获取paramName集合
        List<String> paramNameList = (List<String>) paramMap.get("paramName");
        //获取value集合
        List<String> valueList = (List<String>) paramMap.get("value");
        //遍历记录数
        for (int i = 0; i < paramNameList.size(); i++) {
            GoodsParam goodsParam = new GoodsParam();
            goodsParam.setGoodsId(goodsId);
            goodsParam.setGoodsParamName(paramNameList.get(i));
            goodsParam.setGoodsParamValue(valueList.get(i));
            //插入goods_Param表
            goodsParamMapper.insertSelective(goodsParam);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsDetail(Map<String, Object> map) throws LJCBusinessException {
        //获取商品编号
        long goodsId = Long.parseLong(map.get("goodsId").toString());
        //获取goodsDetailImg对象
        List<String> goodsDetailImgList = (List<String>) map.get("goodsDetailImg");
        if (goodsDetailImgList.size() > 0) {
            //如果商品详情图被修改则执行
            for (String s : goodsDetailImgList) {
                GoodsDetail goodsDetail = new GoodsDetail();
                String s1 = uploadImg(s);
                goodsDetail.setGoodsId(goodsId);
                goodsDetail.setGoodsDetailImg(s1);
                //插入goods_detail表
                goodsDetailMapper.insertSelective(goodsDetail);
            }
        }
        String goodsDesc = map.get("goodsDesc").toString();
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setGoodsDesc(goodsDesc);
        goodsMapper.updateByPrimaryKeySelective(goods);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsMarketing(Map<String, Object> map) {
        //获取marketing对象
        String marketingString = JsonUtil.objectToString(map.get("marketing"));
        Marketing marketing = JsonUtil.stringToObject(marketingString, Marketing.class);
        marketingMapper.updateByPrimaryKeySelective(marketing);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsRecommend(Map<String, Object> map) {
        long goodsId = Long.parseLong(map.get("goodsId").toString());
        //获取associateGoods对象
        List<String> associateGoodsList = (List<String>) map.get("associateGoods");
        if (associateGoodsList.size() > 0) {
            //如果关联商品被修改了就先删除associateGoods表的数据再重新添加
            Example example = new Example(AssociateGoods.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("isAssociatedGoods", goodsId);
            associateGoodsMapper.deleteByExample(example);
            //遍历关联商品记录数
            for (String s : associateGoodsList) {
                AssociateGoods associateGoods = new AssociateGoods();
                associateGoods.setIsAssociatedGoods(goodsId);
                associateGoods.setAssociatedGoods(Long.parseLong(s));
                //拆入associateGoods表
                associateGoodsMapper.insertSelective(associateGoods);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map selectClassification(String nameOrKeyword) {
        ArrayList<ArrayList> list = new ArrayList<>();
        Example example = new Example(Item.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(nameOrKeyword)) {
            //如果搜索条件不为空
            criteria.orLike("itemName", "%" + nameOrKeyword + "%");
            criteria.orLike("itemKeyWord", "%" + nameOrKeyword + "%");
            Example.Criteria and = example.and();
            and.andEqualTo("storeId", storeId);
            //获取按条件查出的item集合
            List<Item> items = itemMapper.selectByExample(example);
            ArrayList<HashMap<String, Object>> arrayList = new ArrayList<>();
            for (Item item : items) {
                HashMap<String, Object> map = new HashMap<>(10);
                Example goodsExample = new Example(Goods.class);
                Example.Criteria goodsCriteria = goodsExample.createCriteria();
                int size = 0;
                switch (item.getItemRank()) {
                    case 1:
                        goodsCriteria.orEqualTo("goodsFirstItemId", item.getItemId());
                        //查询此1级分类的所有商品数量
                        size = goodsMapper.selectByExample(goodsExample).size();
                        map.put("firstItem", item);
                        break;
                    case 2:
                        goodsCriteria.orEqualTo("goodsSecondItemId", item.getItemId());
                        //查询此2级分类的所有商品数量
                        size = goodsMapper.selectByExample(goodsExample).size();
                        map.put("secondItem", item);
                        break;
                    case 3:
                        goodsCriteria.orEqualTo("goodsThirdItemId", item.getItemId());
                        //查询此3级分类的所有商品数量
                        size = goodsMapper.selectByExample(goodsExample).size();
                        map.put("thirdItem", item);
                        break;
                    default:
                        break;
                }
                map.put("num", size);
                arrayList.add(map);
            }
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("200");
            jsonResult.setField("data", arrayList);
            return jsonResult.getMap();
        } else {
            criteria.andEqualTo("storeId", storeId);
            criteria.andEqualTo("itemRank", 1);
            //获取所有1级分类对象
            List<Item> firstItems = itemMapper.selectByExample(example);
            for (Item firstItem : firstItems) {
                HashMap<String, Object> map = new HashMap<>(10);
                ArrayList<HashMap<String, Object>> mapArrayList = new ArrayList<>();
                Example goodsExample = new Example(Goods.class);
                Example.Criteria goodsCriteria = goodsExample.createCriteria();
                goodsCriteria.orEqualTo("goodsFirstItemId", firstItem.getItemId());
                //查询此1级分类的所有商品数量
                int size = goodsMapper.selectByExample(goodsExample).size();
                map.put("num1", size);
                map.put("firstItem", firstItem);
                Example example1 = new Example(Item.class);
                Example.Criteria criteria1 = example1.createCriteria();
                criteria1.andEqualTo("itemParentId", firstItem.getItemId());
                //获取此类1级分类下的所有2级分类对象
                List<Item> secondItems = itemMapper.selectByExample(example1);
                for (Item secondItem : secondItems) {
                    HashMap<String, Object> map1 = new HashMap<>(10);
                    Example goodsExample1 = new Example(Goods.class);
                    Example.Criteria goodsCriteria1 = goodsExample1.createCriteria();
                    goodsCriteria1.orEqualTo("goodsSecondItemId", secondItem.getItemId());
                    //查询此2级分类的所有商品数量
                    int size1 = goodsMapper.selectByExample(goodsExample1).size();
                    map1.put("num2", size1);
                    map1.put("secondItem", secondItem);
                    Example example2 = new Example(Item.class);
                    Example.Criteria criteria2 = example2.createCriteria();
                    criteria2.andEqualTo("itemParentId", secondItem.getItemId());
                    //获取此类2级分类下的所有3级分类对象
                    List<Item> thirdItems = itemMapper.selectByExample(example2);
                    for (Item thirdItem : thirdItems) {
                        HashMap<String, Object> map2 = new HashMap<>(10);
                        Example goodsExample2 = new Example(Goods.class);
                        Example.Criteria goodsCriteria2 = goodsExample2.createCriteria();
                        goodsCriteria2.orEqualTo("goodsThirdItemId", thirdItem.getItemId());
                        //查询此3级分类的所有商品数量
                        int size2 = goodsMapper.selectByExample(goodsExample2).size();
                        map2.put("num3", size2);
                        map2.put("thirdItem", thirdItem);
                        mapArrayList.add(map2);
                    }
                    mapArrayList.add(map1);
                }
                mapArrayList.add(map);
                list.add(mapArrayList);
            }
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("200");
            jsonResult.setField("data", list);
            return jsonResult.getMap();
        }
    }

    @Override
    public Map getClassificationInfo(Long itemId) {
        Item item = itemMapper.selectByPrimaryKey(itemId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", item);
        return jsonResult.getMap();
    }

    @Override
    public Map editClassificationInfo(Map<String, Object> map) throws LJCBusinessException {
        String itemString = JsonUtil.objectToString(map.get("item"));
        Item item = JsonUtil.stringToObject(itemString, Item.class);
        if (!StringUtils.isEmpty(item.getItemImg())){
            //如果分类图标被修改了就执行
            String s = uploadImg(item.getItemImg());
            item.setItemImg(s);
        }
        itemMapper.updateByPrimaryKeySelective(item);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map selectLabelGroup(String name) {
        Example example = new Example(LabelGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andEqualTo("labelGroupStatus", true);
        if (!StringUtils.isEmpty(name)){
            criteria.andLike("labelGroupName", "%" + name + "%");
        }
        //获取所有标签组对象
        List<LabelGroup> labelGroups = labelGroupMapper.selectByExample(example);
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (LabelGroup labelGroup : labelGroups) {
            HashMap<String, Object> map = new HashMap<>(10);
            Example example1 = new Example(Label.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("labelsGroupId", labelGroup.getGoodsLabelGroupId());
            List<Label> labels = labelMapper.selectByExample(example1);
            //获取标签组里标签的数量
            int size = labels.size();
            ArrayList<String> list1 = new ArrayList<>();
            for (Label label : labels) {
                list1.add(label.getLabelsName());
            }
            map.put("num", size);
            map.put("labelGroup", labelGroup);
            map.put("labelName", list1);
            list.add(map);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", list);
        return jsonResult.getMap();
    }

    @Override
    public Map activateOrDisabledLabelGroup(Map<String, Object> map, Boolean way) {
        List<String> labelGroupIdList = (List<String>) map.get("labelGroupId");
        goodsMapper.activateOrDisabledLabelGroup(labelGroupIdList, way);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @Override
    public Map deleteLabelGroup(Map<String, Object> map) {
        List<String> labelGroupIdList = (List<String>) map.get("labelGroupId");
        for (String s : labelGroupIdList) {
            //修改商品表的标签组编号为null
            goodsMapper.updateGoodsLabelGroupId(s);
            Example example = new Example(Label.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("labelsGroupId", Long.parseLong(s));
            //删除label表中的商品组数据
            labelMapper.deleteByExample(example);
            //删除labelGroup表中的数据
            labelGroupMapper.deleteByPrimaryKey(Long.parseLong(s));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editLabelGroup(Map<String, Object> map) {
        //获取标签组对象
        String jsonString = JsonUtil.objectToString(map.get("labelGroup"));
        LabelGroup labelGroup = JsonUtil.stringToObject(jsonString, LabelGroup.class);
        //修改label_group表中的数据
        labelGroupMapper.updateByPrimaryKeySelective(labelGroup);
        //删除label表中的数据再重新添加
        Example example = new Example(Label.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("labelsGroupId", labelGroup.getGoodsLabelGroupId());
        labelMapper.deleteByExample(example);
        //获取标签名集合
        List<String> labelsList = (List<String>) map.get("labelName");
        //遍历标签名集合进行插入表
        for (String s : labelsList) {
            Label label = new Label();
            label.setLabelsName(s);
            label.setLabelsGroupId(labelGroup.getGoodsLabelGroupId());
            labelMapper.insertSelective(label);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map addGoodsGroup(Map<String, Object> map) throws LJCBusinessException {
        //获取商品组对象
        String jsonString = JsonUtil.objectToString(map.get("goodsGroup"));
        GoodsGroup goodsGroup = JsonUtil.stringToObject(jsonString, GoodsGroup.class);
        goodsGroup.setStoreId(storeId);
        //获取商品组名
        String goodsGroupName = goodsGroup.getGoodsGroupName();
        Example example = new Example(GoodsGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("goodsGroupName", goodsGroupName);
        //通过商品组名查询数据库是否已存在名称相同的数据
        List<GoodsGroup> goodsGroups = goodsGroupMapper.selectByExample(example);
        //判断新添加的商品组名是否存在
        if (goodsGroups.size() > 0) {
            //如果存在则抛出异常
            throw new LJCBusinessException(LJCExceptionCode.GOODS_GROUP_NAME_EXIST);
        }
        //插入表
        goodsGroupMapper.insertSelective(goodsGroup);
        //获取插入标签组的编号
        List<GoodsGroup> goodsGroups1 = goodsGroupMapper.selectByExample(example);
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        //遍历商品编号集合进行插入表
        for (String s : goodsIdList) {
            Goods goods = new Goods();
            goods.setGoodsId(Long.parseLong(s));
            goods.setGoodsGroupId(goodsGroups1.get(0).getGoodsGroupId());
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map selectGoodsGroup(String goodsGroupName) {
        Example example = new Example(GoodsGroup.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId", storeId);
        criteria.andEqualTo("goodsGroupStatus", true);
        if(!StringUtils.isEmpty(goodsGroupName)){
            criteria.andLike("goodsGroupName", "%" + goodsGroupName + "%");
        }
        //通过条件获取所有商品组对象集合
        List<GoodsGroup> goodsGroups = goodsGroupMapper.selectByExample(example);
        ArrayList<HashMap<String, Object>> list = new ArrayList<>();
        for (GoodsGroup goodsGroup : goodsGroups) {
            HashMap<String, Object> map = new HashMap<>(10);
            Example example1 = new Example(Goods.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("goodsGroupId", goodsGroup.getGoodsGroupId());
            //查询所有商品组编号为此的商品信息
            List<Goods> goods = goodsMapper.selectByExample(example1);
            map.put("num", goods.size());
            map.put("goodsGroups", goodsGroup);
            list.add(map);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setField("data", list);
        return jsonResult.getMap();
    }

    @Override
    public Map activateOrDisabledGoodsGroup(Map<String, Object> map, Boolean way) {
        //获取商品组编号集合
        List<String> goodsGroupIdList = (List<String>) map.get("goodsGroupId");
        for (String s : goodsGroupIdList) {
            GoodsGroup goodsGroup = new GoodsGroup();
            goodsGroup.setGoodsGroupId(Long.parseLong(s));
            if (way){
                //启用
                goodsGroup.setGoodsGroupStatus(true);
            }else {
                //禁用
                goodsGroup.setGoodsGroupStatus(false);
            }
            goodsGroupMapper.updateByPrimaryKeySelective(goodsGroup);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @Override
    public Map editGoodsGroup(Map<String, Object> map) {
        //获取商品组对象
        String jsonString = JsonUtil.objectToString(map.get("goodsGroup"));
        GoodsGroup goodsGroup = JsonUtil.stringToObject(jsonString, GoodsGroup.class);
        //修改goods_group表中的数据
        goodsGroupMapper.updateByPrimaryKeySelective(goodsGroup);
        //获取商品编号集合
        List<String> goodsIdList = (List<String>) map.get("goodsId");
        //遍历商品编号集合进行插入表
        for (String s : goodsIdList) {
            //先清空商品表中的商品组编号
            Example example = new Example(Goods.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("goodsGroupId", goodsGroup.getGoodsGroupId());
            //获取所有商品组编号为此商品组编号的商品对象集合
            List<Goods> goods1 = goodsMapper.selectByExample(example);
            for (Goods goods : goods1) {
                //设置商品的商品组编号为null再修改
                goods.setGoodsGroupId(null);
                goodsMapper.updateByPrimaryKeySelective(goods);
            }
            //修改商品表数据
            Goods goods = new Goods();
            goods.setGoodsId(Long.parseLong(s));
            goods.setGoodsGroupId(goodsGroup.getGoodsGroupId());
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("保存成功");
        return jsonResult.getMap();
    }

    @Override
    public Map deleteGoodsGroup(Map<String, Object> map) {
        List<String> goodsGroupIdList = (List<String>) map.get("goodsGroupId");
        for (String s : goodsGroupIdList) {
            //先清空商品表中的商品组编号
            Example example = new Example(Goods.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("goodsGroupId", Long.parseLong(s));
            //获取所有商品组编号为此商品组编号的商品对象集合
            List<Goods> goods1 = goodsMapper.selectByExample(example);
            for (Goods goods : goods1) {
                //设置商品的商品组编号为null再修改
                goods.setGoodsGroupId(null);
                goodsMapper.updateByPrimaryKeySelective(goods);
            }
            //删除goods_Group表中的数据
            goodsGroupMapper.deleteByPrimaryKey(Long.parseLong(s));
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getMap();
    }

    private String uploadImg(String imgPath) throws LJCBusinessException {
        //获取原始文件名
        String oldFileName = imgPath.substring(37);
        String prefix = "temporary/";
        //判断上传的分类图标是否存在
        if (!OSSUtil.fileExist(prefix + oldFileName)) {
            throw new LJCBusinessException(LJCExceptionCode.ICON_IS_NOT_EXIST);
        }
        //将文件从临时文件夹复制到新路径，并删除原文件
        OSSUtil.copyFile(prefix + oldFileName, "shopImg/" + oldFileName);
        OSSUtil.delete(prefix + oldFileName);
        String realImgPath = OSSController.BUCKET + "shopImg/" + oldFileName;
        return realImgPath;
    }
}
